package com.schoolCommunity.util;

import com.schoolCommunity.entity.ConfessionWall;
import com.schoolCommunity.mapper.ConfessionWallMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

//表白墙的redis操作
@Component
public class ConfessionWallRedis {

    @Autowired
    @Qualifier("MyTemplate")
    private RedisTemplate redisTemplate;
    @Resource
    private com.schoolCommunity.mapper.ConfessionWallMapper ConfessionWallMapper;

    //从redis获取指定期数的表白墙,若没有数据会返回null
    public List<ConfessionWall> getAppointConfessionWallRedis(long periods) {

        Object o = redisTemplate.opsForValue().get("getAppointConfessionWall:" + periods);
        if (o == null) {
            return null;
        } else {
            List<ConfessionWall> list = (List<ConfessionWall>) o;
            return list;
        }

    }

    //redis写入指定期数表白墙
    public boolean setAppointConfessionWallRedis(List<ConfessionWall> list, long periods) {

        try {
            //写入redis，时间为1天
            redisTemplate.opsForValue().set("getAppointConfessionWall:" + periods, list, 1, TimeUnit.DAYS);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    //redis记录点赞某一个表白墙的用户
    public boolean setMemberAddLoves(long confessionWallId, long memberId) {

        try {
            redisTemplate.opsForValue().set("AddLoves:" + confessionWallId + ":" + memberId, 1, 1, TimeUnit.DAYS);
            return true;

        } catch (Exception e) {
            return false;
        }
    }

    //redis 获取是否存在某个人的点赞记录（一般存放一天）
    public boolean getMemberAddLoves(long confessionWallId, long memberId) {
        return redisTemplate.hasKey("AddLoves:" + confessionWallId + ":" + memberId);
    }


}
